home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: returning a string from a function
- Date: 12 Jan 1996 07:43 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <12JAN199607430522@erich.triumf.ca>
- References: <4d4uh8$q46@mailhost.mwmicro.com>
- NNTP-Posting-Host: erich.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4d4uh8$q46@mailhost.mwmicro.com>, aschlies@citynet.net writes...
- >I am learning C with the aid of only a book. I am at an inpass in
- >this process. I have a small function that needs to return a string
- >back to the main routine. I have the following prototype:
- >
- >char function_name(char in_string[80])
-
- To return a string, this must be char *function(...)
- >
- >{
- > char value[80];
- >
- > ...value takes on part of the value of the last 10 characters
- > of in_string.
- >
- > return(value);
-
- SERIOUS error! - you are returning a pointer to an automatic variable, which
- becomes invalid when your function terminates. Change "char value[80]" to
- "static char value[80]" to ensure that "value" remains valid after the function
- terminates.
-
- >Also, I wrote the routine that copies the last 10 characters. Is there
- >a lib. function that does that for me?? Did I reinvent the wheel?
-
- There is a strncpy() that copies n chars - it could be used in a function to
- return the last n...
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-
-
-
-
-